/*
 * AbstractSQLWriter.java
 *
 * Created on 12 June 2007, 19:35
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.databean.utils;

import com.grubby.utils.StringUtils;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class provides methods to help writing SQL data to a file.
 *
 * @author A. HARMEL-LAW. E: andrew.harmel.law@gmail.com
 * @version 0.1
 */
public class SQLFileWriterHelper {
    
    /** Singleton */
    private SQLFileWriterHelper() {
    }
    
    /**
     * Write a header comment for the SQL file
     * @param headerContent array containing user defined lines for the header
     * comment.
     * @param sqlWriter Writer for the output
     * @param aRandomSeed Random seed used ro generate the dummy data
     * <P>You should always have at least a one-line description such as the
     * purpose of the dummy data.
     */
    public void writeHeaderComment(String[] headerContent, Writer sqlWriter, 
            long aRandomSeed) {
        
        // Calculate the width of the comment (for the surrounds)
        int commentWidth = 46;
        for (String elem : headerContent) {
            if (elem.length() > commentWidth)
                commentWidth = elem.length();
        }
        
        // Write the header comment
        try {
            
            sqlWriter.write("--- " + StringUtils.paddingString("", commentWidth, '-', true) + "---\n");
            for(String elem : headerContent) {
                sqlWriter.write("--- ");
                sqlWriter.write(StringUtils.paddingString(elem, commentWidth, '-', true));
                sqlWriter.write("---\n");
            }
            sqlWriter.write(StringUtils.paddingString("--- Generated on " + (new Date()).toString(), commentWidth, '-', true) + "---\n");
            sqlWriter.write("--- Random Seed: " + aRandomSeed + "---\n");
            sqlWriter.write("--- " + StringUtils.paddingString("", commentWidth, '-', true) + "---\n");
            sqlWriter.write("\n");
            
        } catch (IOException ex) {
            
            Logger.getLogger(SQLFileWriterHelper.class.getName()).log(Level.SEVERE, null, ex);
            
        }
    }
}